package cn.wildfire.chat.app.extendsion.ui;

import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import cn.wildfire.chat.app.extendsion.utils.StatusBarUtil;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;
import cn.wildfire.chat.app.main.MainActivity;

/**
 * Create by HEKE, 2019/8/29 15:12
 * TODO 主界面右上角更多弹出选择窗口
 */
public class MainMenuPopupWindow implements View.OnClickListener {

    private MainActivity mainActivity;

    private PopupWindow window;
    private View mView;
    private LinearLayout qunliaoView, addView, scanView;

    private boolean showing;

    private void initViews() {
        mView = View.inflate(mainActivity.getApplicationContext(), R.layout.main_menu_window, null);
        qunliaoView = mView.findViewById(R.id.qunliaoLayout);
        addView = mView.findViewById(R.id.addFriendLayout);
        scanView = mView.findViewById(R.id.saoyisaoLayout);
        qunliaoView.setOnClickListener(this);
        addView.setOnClickListener(this);
        scanView.setOnClickListener(this);
        window = new PopupWindow(mainActivity);
        window.setContentView(mView);
        window.setWidth((int) mainActivity.getResources().getDimension(R.dimen.popup_width));
        window.setHeight((int) mainActivity.getResources().getDimension(R.dimen.popup_height));
        window.setFocusable(true);
        window.setTouchable(true);
        window.setOutsideTouchable(true);
        window.setBackgroundDrawable(null);
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                showing = false;
            }
        });
    }

    public MainMenuPopupWindow(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
        initViews();
    }

    public boolean isShowing() {
        return showing;
    }

    public void show(View anchorView) {
        if (isShowing())
            return;
        if (anchorView == null) {
            int statusbarHeight = StatusBarUtil.getStatusBarHeight(mainActivity);
            int toolbarHeight = TextUtils.dip2px(mainActivity, 50);
            window.showAtLocation(mView, Gravity.TOP | Gravity.RIGHT, 16, statusbarHeight + toolbarHeight);
            showing = true;
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                window.showAsDropDown(anchorView, 0, 16, Gravity.BOTTOM);
                showing = true;
            }
        }
    }

    public void hide() {
        if (!isShowing())
            return;
        showing = false;
        window.dismiss();
    }

    @Override
    public void onClick(View view) {
        mainActivity.onOptionsClicked(view.getId());
        hide();
    }

}
